﻿Imports DataAccess
Imports Entities
Public Class PagosBL
    Public Shared Function Save(ByVal zobject As PAGOS) As PAGOS
        If IsNothing(zobject.PAGO) Or zobject.PAGO = 0 Then
            Return PagosDAL.Create(zobject)
        Else
            Return PagosDAL.Update(zobject)
        End If
    End Function

    Public Shared Function List() As List(Of PAGOS)
        Dim lista As New List(Of pagos)
        lista = PagosDAL.List
        Debug.Print("PagosBL.List.Count= " + lista.Count.ToString)
        If lista.Count > 0 Then
            Return lista
        Else
            Return Nothing
        End If
    End Function
 
   
    Public Shared Function ListByVentaId(ByVal zVentaId As Long) As List(Of pagos)
        Dim lista = PagosVentaDAL.ListByVentaId(zVentaId)
        Dim ListaF As New List(Of pagos)

        For Each bolPag In lista
            Dim pago = PagosBL.SearchById(bolPag.Pago)
            If Not IsNothing(pago) Then
                ListaF.Add(pago)
            End If
        Next
        If ListaF.Count > 0 Then
            Return ListaF
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function ListByTipoPago(ByVal zTipoPago As String) As List(Of PAGOS)
        Return PagosDAL.ListByTipoPago(zTipoPago)
    End Function

    Public Shared Function SearchById(ByVal zId As Long) As PAGOS
        Dim bol As New pagos
        bol = PagosDAL.SearchById(zId)
        Return bol
    End Function

    Public Shared Function ListPagosToLIstPgosR(ByVal zlist As List(Of PAGOS)) As List(Of PagoR)
        Dim PagosRList = New List(Of PagoR)
        For Each pago In zlist
            If Not IsNothing(pago) Then PagosRList.Add(PagosToPagoR(pago))
        Next
        If PagosRList.Count > 0 Then
            Return PagosRList
        Else
            Return Nothing
        End If
    End Function
    Public Shared Function ListPagosR(ByVal zVentaId As Long) As List(Of PagoR)
        Dim lista As New List(Of pagos)
        Dim listaF As New List(Of PagoR)
        lista = ListByVentaId(zVentaId)
        If Not IsNothing(lista) Then
            For Each pago In lista
                listaF.Add(PagosToPagoR(pago))
            Next
        End If
        If listaF.Count > 0 Then
            Return listaF
        Else
            Return Nothing
        End If
    End Function

    Private Shared Function PagosToPagoR(ByVal zPago As PAGOS) As PagoR
        Dim pagR As New PagoR
        With pagR
            .PagoId = zPago.PAGO
            .FormaPagoId = zPago.FORMAPAGO
            .Fecha = CDate(zPago.FECHA).ToShortDateString
            .Hora = CDate(zPago.FECHA).ToShortTimeString
            .FormaPago = FormaPagoBL.GetDescripcion(zPago.FORMAPAGO)
            .Monto = zPago.MONTO
            .Tipo = zPago.TIPO

        End With
        Return pagR
    End Function

   

    Public Shared Function UpdatePagos(ByVal zVentaId As Long, ByVal zPagosList As List(Of PagoR)) As List(Of PAGOS)
        Dim pagosList = PagosBL.ListByVentaId(zVentaId)
        Dim listaf As New List(Of PAGOS)

        For Each pago In pagosList
            If Not IsNothing(pago) Then
                Dim pagoId = pago.PAGO
                Dim pag = zPagosList.Where(Function(f) f.PagoId = pagoId).SingleOrDefault
                If Not IsNothing(pag) Then
                    pago.FORMAPAGO = pag.FormaPagoId
                    pago.MONTO = pag.Monto
                    pago.FECHA = pag.Fecha
                    listaf.Add(PagosDAL.Update(pago))
                Else
                    Delete(pago)

                End If
            End If
        Next
        If listaf.Count > 0 Then
            Return listaf
        Else
            Return Nothing
        End If
    End Function

    Private Shared Function Delete(ByVal zPago As PAGOS) As Boolean
        Dim bolPag = PagosVentaDAL.SearchByPagoId(zPago.PAGO)
        PagosDAL.Delete(zPago)
        If Not IsNothing(bolPag) Then
            PagosVentaDAL.Delete(bolPag)
        End If
        Return True
    End Function
End Class
